import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from 'views/Login'
import Register from 'views/Register'
import UserInfo from 'views/userInfo'
import Edit from 'views/Edit'
import HomeBilibili from 'views/HomeBilibili'
import Article from 'views/Article'
Vue.use(VueRouter)

  const routes = [
    {
      path:'/home',
      component:HomeBilibili,
      meta: {
        keepalive:true
      }
    },
    {
      path:'/login',
      component:Login
    },
    {
      path:'',
      redirect:'/home'
    },
    {
      path:'/register',
      component: Register
    },
    {
      path: '/userInfo',
      component: UserInfo,
      meta: true,
    },
    {
      path:'/edit',
      component:Edit
    },
    {
      path:'/article/:id',
      component:Article
    }
]


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to,from,next) => {
  if (!localStorage.getItem('id')&&!localStorage.getItem('token')&&to.meta===true) {
    router.push('/login')
    Vue.prototype.$toast.fail('登录失效')
    return
  }
  next()
})

export default router
